<?php namespace Summer\Hjd\Models;

use Model;

/**
 * Model
 */
class WalletModel extends Model
{
    use \Winter\Storm\Database\Traits\Validation;
    /**
     * @var string The database table used by the model.
     */
    public $table = 'summer_hjd_wallet';

    /**
     * @var array Validation rules
     */
    public $rules = [
    ];
    protected $fillable = [
        'user_id',
        'currency_slug',
        'balance',
        'status',
    ];
    public $belongsTo = [
        'currency' => ['Summer\Hjd\Models\CurrencyModel', 'key' => 'currency_slug', 'otherKey' => 'slug']
    ];
    /**
     * 初始化钱包
     * @param $user_id
     */
    public function initWallet($user_id){
        $currencies=CurrencyModel::where('status',1)->get();
        foreach ($currencies as $cv){
            $table_c=WalletModel::where('user_id',$user_id)->where('currency_slug',$cv->slug)->first();
            if(!$table_c){
                WalletModel::create([
                    'user_id'=>$user_id,
                    'currency_slug'=>$cv->slug,
                    'balance'=>0,
                    'status'=>1,
                ]);
            }
        }
    }
}
